const axios=require('axios');
const {JSDOM}=require('jsdom')
const dayjs=require('dayjs')
const constant=require('./config')
/**
 * @param weekNumber 周数
 * @param acount 教务处用户名，默认张正德账号
 * @param password 教务处密码
 */
const getScdeduleData=async (weekNumber,acount=constant.account,password=constant.password)=>{
  const encodedData = Buffer.from(acount).toString('base64')+"%%%"+Buffer.from(password).toString('base64');
    // 第一次请求，获取登录页面的 Cookies
    let result;
    const cookieRes=await axios.get('https://www.hltz.net/hlxy_jsxsd/')
    const cookie=cookieRes.headers['set-cookie']
    const data={
      loginMethod:"LoginToXk",
      userAccount: acount,
      encoded:encodedData
    }
    const loginRes=await axios.post("https://www.hltz.net/hlxy_jsxsd/xk/LoginToXk",data,{
        headers:{
          'Content-Type':"application/x-www-form-urlencoded",
          'Cookie': cookie
        },
        maxRedircts:0,
      })
      const loginCookie=loginRes.config.headers["Cookie"]
      const selectRes=await axios.get("https://www.hltz.net/hlxy_jsxsd/framework/xsMainV_new.htmlx?t1=1",{headers:{
          'Cookie': loginCookie
        }})
      const selectDom=new JSDOM(selectRes.data).window.document
      const select=selectDom.getElementsByTagName("select")
      const yearOption=select[0].getElementsByTagName("option")
      const weekOption=select[1].getElementsByTagName("option")
      const rq=weekOption[weekNumber+1].value
      const xnxqid=yearOption[0].innerHTML
      const res=await axios.get(`https://www.hltz.net/hlxy_jsxsd/framework/mainV_index_loadkb.htmlx?rq=${rq}&sjmsValue=${constant.sjmsValue}&xnxqid=${xnxqid}&xswk=false`,{
        headers:{
          'Cookie': loginCookie
        },
      })
      const dom=new JSDOM(res.data).window.document
      const tbody=dom.getElementsByTagName("tbody")
      const trs=tbody[0].getElementsByTagName("tr")
      const trsResult=[]
        for(let i=0;i<trs.length;i++){
          const tr=trs[i]
          const tds=tr.getElementsByTagName("td")
          const tdsResult=[]
          for(let j=0;j<tds.length;j++){
            const td=tds[j]
            if(td.innerHTML){
              const itemBox=td.getElementsByClassName("item-box")
              const box=itemBox[0]
              if(box){
                const p=box.getElementsByTagName("p")
                const tch_name=box.getElementsByClassName("tch-name")[0].getElementsByTagName("span")
                const location_time=box.getElementsByTagName("div")[1].getElementsByTagName("span")
                const time=location_time[1].textContent.split(" ")
                tdsResult.push({
                  subject:p[0].innerHTML,
                  credit:tch_name[0].innerHTML,
                  number:tch_name[1].innerHTML,
                  location:location_time[0].textContent,
                  week:time[0],
                  weekNumber:time[1]
                })
              }else{
                tdsResult.push(td.innerHTML.trim())
              }
            }else{
              tdsResult.push("")
            }
          }
          trsResult.push(tdsResult)
        }
      return trsResult
}

module.exports={
  getScdeduleData,
}
